#encoding:UTF-8
import mylib
import re
import MySQLdb
import pythonlibSB

conn = mylib.create_conn()


def get_lichtivi():
    
    results = get_url_lichtivi()
    datetime_today = mylib.get_datetime_today()
    print results
    for r in results:
        url,id_kenh,url_time = r[0],int(r[1]),r[2]
        print url
        content = mylib.get_data(url)
        if id_kenh in (1,2,3,4,5):
            print id_kenh
            pos1 = content.find('style="text-align: center"')
            pos2 = content.find('</table>',pos1)
            get_str = content[pos1:pos2]
#            print get_str
            newpos = get_str.find('<table')
            get_str = get_str[newpos:]
#            print get_str
            last_str = process_table(get_str,'size\\s*=\\s*["]2["][>]([^><]+)[<][/]')
            last_str = pythonlibSB.chuanHoaHtml(last_str)
            insert_lichtivi(id_kenh, url_time, last_str, datetime_today)
            print "id ========= "+str(id_kenh)
            
            
        elif id_kenh == 6:
            
            get_str = pythonlibSB.s_string(content,'style="border-collapse:','table')
            last_str = process_table(get_str, 'size\\s*=\\s*["]2["][>]([^><]+)[<][/]')
            last_str = pythonlibSB.chuanHoaHtml(last_str)
            insert_lichtivi(id_kenh, url_time, last_str, datetime_today)
            print "id ========= "+str(id_kenh)
            

def insert_lichtivi(id_kenh,url_time,content,content_time):
    try:
        content = str(MySQLdb.escape_string(content))
        cmd = "CALL insert_lichtivi('%s','%s','%s','%s')"%(str(id_kenh),url_time,content,content_time)
        print cmd
        cursor = conn.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute(cmd)
        cursor.close()
        print "insert thanh cong"
    except:
        print "loi insert"
    


def process_table(in_str,regex):
    
    try:
        reg = re.compile('<strong style="font-weight: 400"><nobr>')
        for match in reg.findall(in_str):
            in_str = in_str.replace(match,'')
        reg = re.compile('<strong style="font-weight: 400">')
        for match in reg.findall(in_str):
            in_str = in_str.replace(match,'')
        reg = re.compile('</strong> ')
        for match in reg.findall(in_str):
            in_str = in_str.replace(match,'')
        reg = re.compile('</nobr>')
        for match in reg.findall(in_str):
            in_str = in_str.replace(match,'')
        reg = re.compile('<span style="font-weight: 400">')
        for match in reg.findall(in_str):
            in_str = in_str.replace(match,'')
        reg = re.compile('<font color="#FF0000">')
        for match in reg.findall(in_str):
            in_str = in_str.replace(match,'')
        reg = re.compile('<strong>')
        for match in reg.findall(in_str):
            in_str = in_str.replace(match,'')
        reg = re.compile('<span id="dtx-highlighting-item2">')
        for match in reg.findall(in_str):
            in_str = in_str.replace(match,'')
        reg = re.compile('<span id="dtx-highlighting-item1">')
        for match in reg.findall(in_str):
            in_str = in_str.replace(match,'')
        reg = re.compile('<br />')
        for match in reg.findall(in_str):
            in_str = in_str.replace(match,'')
        reg = re.compile('</span>')
        for match in reg.findall(in_str):
            in_str = in_str.replace(match,'')
        reg = re.compile('<span id="dtx-highlighting-item5">')
        for match in reg.findall(in_str):
            in_str = in_str.replace(match,'')
        reg = re.compile('<span id="dtx-highlighting-item4">')
        for match in reg.findall(in_str):
            in_str = in_str.replace(match,'')
        last_str = ""
        start = in_str.find('<tr')
        while start != -1:
    
            end = in_str.find('</tr>',start)
            if end == -1:
                break
            t = in_str[start:end]
            reg  = re.compile('size\\s*=\\s*["]2["][>]([^><]+)[<][/]')
            x =reg.findall(t)
            if len(x) > 0:
                new_str = "<p>"
                for i in x:
                    new_str = new_str+str(i)+" "
                new_str = new_str + "</p>"
                last_str = last_str + new_str
                
            start = end + 1
        
        return last_str
    except:
        return ""
            

def get_url_lichtivi():
    try:
        results = []
        
        cmd = "SELECT * FROM tbl_lichtivi WHERE `status` = 0;"
        url_results = mylib.get_result_sql(conn, cmd)
        for u in url_results:
            url = u['url_lichtivi']
            id_kenh = u['id_kenh']
            url_time = u['url_time']
            results.append((url,id_kenh,url_time))
            
        return results
    except:
        print "Ko lay dc url"
        return []

if __name__ == "__main__":
    get_lichtivi()